<?php
// +----------------------------------------------------------------------
// | Name: 正义CMS管理系统 [ 为了快速搭建软件应用而生的，希望能够帮助到大家提高开发效率。 ]
// +----------------------------------------------------------------------
// | Copyright: (c) 2024~2024 https://www.hczycm.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed: 这是一个自由软件，允许对程序代码进行修改，但希望您留下原有的注释。
// +----------------------------------------------------------------------
// | Author: 韩城正义传媒  <kefu@hczycm.com>
// +----------------------------------------------------------------------
// | Version: V1
// +----------------------------------------------------------------------

/**
 * @Name
 * @Description
 * @Auther 韩城正义传媒
 * @Date 2024/6/2 17:16
 */

namespace Modules\Common\Exceptions;

use ErrorException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use ParseError;
use PHPUnit\Runner\InvalidOrderException;
use ReflectionException;
use RuntimeException;
use Throwable;
class Handler extends ExceptionHandler
{
    private $status = 0;
    private $message = '';
    /**
     * The list of the inputs that are never flashed to the session on validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }

    private function setErrorException($e){
        if($e instanceof ParseError) {
            $this->status = 50001;
            $this->message = "服务器语法错误";
        } else if($e instanceof ReflectionException) {
            $this->status = 50002;
            $this->message = "服务器异常映射";
        }
        if($e instanceof RuntimeException) {
            $this->status = 50003;
            $this->message = "服务器运行期间错误";
        }
        if($e instanceof ErrorException) {
            $this->status = 50004;
            $this->message = "服务器框架运行错误";
        }
        if($e instanceof InvalidArgumentException) {
            $this->status = 60001;
            $this->message = "数据库连接错误";
        }
        if($e instanceof ModelNotFoundException) {
            $this->status = 60002;
            $this->message = "数据库模型错误";
        }
        if($e instanceof QueryException) {
            $this->status = 60002;
            $this->message = "数据库DB错误";

        }
    }
    public function render($request, Throwable $e)
    {

        if($request->is('api/*')) {
            $this->setErrorException($e);
            if($this->status){
                $data=[
                    "file"=>$e->getFile(),
                    "line"=>$e->getLine(),
                ];
                if($this->status == 6001){
                    $data['message'] = $e->getModel();
                }else{
                    $data['message'] = $e->getMessage();
                }
                return response()->json([
                    'status' => $this->status,
                    'message' => env("APP_DEBUG") ? $this->message :MessageData::BAD_REQUEST,
                    'data' => $data
                ],CodeData::INTERNAL_SERVER_ERROR);
            }
            if($e instanceof ApiException){
                $result = [
                    'status' => $e->getCode(),
                    'message' => $e->getMessage(),

                ];
                return response()->json($result,CodeData::INTERNAL_SERVER_ERROR);
            } else if($e instanceof ValidationException){
                $result = [
                    'status' => StatusData::BAD_REQUEST,
                    'message' => array_values($e->errors())[0][0],

                ];
                return response()->json($result,CodeData::BAD_REQUEST);
            }
        }
        return parent::render($request, $e); // TODO: Change the autogenerated stub
    }
}
